Skip to content

Fix PXI I2C size decode - #976

Merged
Wolfvak merged 3 commits into
d0k3:masterfrom
rosaage:master
Oct 19, 2026
Merged

Wolfvak merged 3 commits into
d0k3:masterfrom
rosaage:master

Conversation

@rosaage

@rosaage rosaage commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

During some testing I discovered changing SHMEM_BUFFER_SIZE from 2048 to 20480 broke I2C transfers.
This was due to size not being calculated correctly.

Test code:

This was tested using onlinegdb.com:

#include <stdio.h>
#include <stdint.h>

#define SHMEM_BUFFER_SIZE 20480
#define BIT(x) (1 << (x))

int main()
{
  uint32_t var = 0x01FF;
  var = var | (22 << 16); // "size" sent from arm9
  var = var | BIT(31); // Write operation
  uint32_t old_size = (var >> 16) % SHMEM_BUFFER_SIZE;
  uint32_t new_size = ((var >> 16) & 0x7FFF) % SHMEM_BUFFER_SIZE;

  printf("Old size: %01d, New size: %01d", old_size, new_size);

  return 0;
}

Output of the test code with size set to 22:
Old size: 12310, New size: 22

The second commit adds a 15-bit size check to the arm9 side, rejecting commands early if size is too big.

Comment thread arm9/source/system/i2c.c Outdated
Comment on lines +12 to +15
if (size >= SHMEM_BUFFER_SIZE)
return false;
if (size > 0x7FFF)
return false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather see something like _Static_assert(SHMEM_BUFFER_SIZE < BIT(15)); rather than the two runtime checks here, but other than that it looks perfect

the "proper" way to fix it would be to define some types in shmem.h that describe the bitfield layout but that might be a bit too many changes, I'd rather have this as it's only used once or twice

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this potentially not work correctly?
if SHMEM_BUFFER_SIZE is 2048 (current value) and you try to read/write 4096 bytes, then nothing will stop that with a static assert of SHMEM_BUFFER_SIZE < BIT(31)

@Wolfvak

Wolfvak commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the debug and fix, was a really stupid mistake on my part! (also sorry for the delay lol)

@Wolfvak
Wolfvak merged commit d645a4e into d0k3:master Oct 19, 2026
1 check passed
@rosaage

rosaage commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

@Wolfvak I was a bit slow on this (sorry), I was planning on making one more commit to it. Did you read my last comment?

@Wolfvak

Wolfvak commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Yes, I consider this to be "not worse than before" and there's still a bugfix included, so I didn't consider the size <= SHMEM_BUFFER_SIZE check to be absolutely required

Feel free to open another PR that adds that check, at your own pace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants